# 实现多个接口联调对应的测试报告
import requests


class WorkFlowNewTestV2:
    def user_interface_test(self, url, user_info, exp_result, name):
        # 定义字典数据，存储实际接口响应结果和测试结论
        result_data = {}
        # 发送请求
        res = requests.post(url, data=user_info).text
        print(res)
        result_data["接口返回值"] = res
        r = res.find(exp_result)
        if r > 0:
            print(name + "测试通过")
            result_data["测试结论"] = "测试通过"
        else:
            print(name + "测试不通过")
            result_data["测试结论"] = "测试不通过"
        return result_data

    # 把相关的测试结论写入测试报告
    def result_report(self, report_name, res):
        file = open(report_name, "a")
        for k, v in res.items():
            file.write(f"{k},{v},")
        file.close()


if __name__ == '__main__':
    obj = WorkFlowNewTestV2()
    url = "http://129.211.129.101:9006/user/login.do"
    # 正确的用户数据
    user_info = {
        "username": "lllljl",
        "password": "111111"
    }
    exp_result = "登录成功"
    name = "用户登录接口"
    result_data = obj.user_interface_test(url, user_info, exp_result, name)
    obj.result_report("../data/testresult2.csv", result_data)
